
import json
import time
from flask import Flask
from flask import request
from flask import jsonify


import logging


logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger(__name__)


app = Flask(__name__)

'''{ckdwdm:xx,kfdm:xx,wd:xx,sd:xx,pjwd:xx,pjsd:xx,wdbjcs:xx,sdbjcs:xx, wdzdfz:xx,wdzxfz:xx,sdzdfz:xx,sdzxfz:xx,tzurl:xx}'''

@app.route('/setKfwsd', methods = ["POST"]) # 
def setKfwsd():

    logger.info(request.form)
    logger.info(request.get_data().decode())
    logger.info(request.headers)


    # 必传参数

    ckdwdm = request.form.get('ckdwdm') # 仓库单位代码
    kfdm = request.form.get('kfdm') # 库房代码
    wd = request.form.get('wd') # 当前温度
    sd = request.form.get('sd') # 当前湿度

    if not (ckdwdm and kfdm and wd and sd):

        return jsonify({
            'status': -1,
            'errorCode': 1000,
            'errorMsg': "缺少参数",
            'resultBody': None,
        })

    # 非必传参数
    pjwd = request.form.get('pjwd') # 平均温度
    pjsd = request.form.get('pjsd') # 平均湿度
    wdbjcs = request.form.get('wdbjcs') # 温度报警次数
    sdbjcs = request.form.get('sdbjcs') # 湿度报警次数
    wdzdfz = request.form.get('wdzdfz') # 温度最大阀值
    wdzxfz = request.form.get('wdzxfz') # 温度最小阀值
    sdzdfz = request.form.get('sdzdfz') # 湿度最大阀值
    sdzxfz = request.form.get('sdzxfz') # 湿度最小阀值
    tzurl = request.form.get('tzurl') # 库房温湿度态势地址

    logger.info("接收到温湿信息：{}".format(dict(request.form)))

    # return jsonify(
    #     {
    #         'status': 1,
    #         'errorCode': None,
    #         'errorMsg': None,
    #         'resultBody': "success",
    #     }
    # )

    return jsonify(
        {
            "status": 200,
            "success": True
        }
    )

    
if __name__ == '__main__':

    app.run(debug=True, host="0.0.0.0", port=8030)